package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Task;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class TDao {

	DBHelp<Task> db=new DBHelp<Task>();
	
	public void save(Task task){
		String sql="insert into t_task(id,tname,tdescripe,state,level,begintime,endtime,tcreatetime,employeeid,goalid) values(?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql,task.getId(),task.getTname(),task.getTdescripe(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getTcreatetime(),task.getEmployeeid(),task.getGoalid());
	}
	
	public void update(Task task){
		String sql="update t_task set tname=?,tdescripe=?,state=?,level=?,begintime=?,endtime=?,tcreatetime=?,employeeid=?,goalid=?,progress=? where id=?";
		db.executeSQL(sql,task.getTname(),task.getTdescripe(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getTcreatetime(),task.getEmployeeid(),task.getGoalid(),task.getProgress(),task.getId());
	}
	
	public Task findById(String id){
		String sql="select id,tname,tdescripe,state,level,begintime,endtime,tcreatetime,employeeid,goalid,progress from t_task where id=?";
		return db.queryForObject(sql,new TRowMapper(),id);
	}
	
	public List<Task> findByGoalid(String goalid){
		String sql="select id,tname,tdescripe,state,level,begintime,endtime,tcreatetime,employeeid,goalid,progress from t_task where goalid=?";
		return db.queryForList(sql,new TRowMapper(),goalid);
	}
	
	public List<Task> findByEmployeeid(int employeeid){
		String sql="select id,tname,tdescripe,state,level,begintime,endtime,tcreatetime,employeeid,goalid,progress from t_task where employeeid=?";
		return db.queryForList(sql,new TRowMapper(),employeeid);
	}
	
	private class TRowMapper implements RowMapper<Task>{

		public Task mapperRow(ResultSet rs) throws SQLException {
			Task task=new Task();
			task.setBegintime(rs.getString("begintime"));
			task.setEmployeeid(rs.getInt("employeeid"));
			task.setEndtime(rs.getString("endtime"));
			task.setGoalid(rs.getString("goalid"));
			task.setId(rs.getString("id"));
			task.setLevel(rs.getString("level"));
			task.setState(rs.getString("state"));
			task.setTcreatetime(rs.getString("tcreatetime"));
			task.setTdescripe(rs.getString("tdescripe"));
			task.setTname(rs.getString("tname"));
			task.setProgress(rs.getDouble("progress"));
			return task;
		}
		
	}
}
